//  this library.
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/back_button_widget.dart';

/// 选择考试、科目等标题
class SelectTitleWidget extends StatefulWidget {
  /// 页面上下文控制器
  final BuildContext pageContext;

  /// 标题名称
  final String titleName;

  /// 输入框点击确定时的方法
  final ValueChanged<String> onSubmittedFun;

  const SelectTitleWidget(
      {super.key,
        required this.pageContext,
        required this.titleName,
        required this.onSubmittedFun});

  @override
  State createState() => _SelectTitleWidgetState();
}

class _SelectTitleWidgetState extends State<SelectTitleWidget> {
  /// 输入框焦点控制器
  late FocusNode _focusNode;

  /// 输入框对应控制器
  late TextEditingController _textEditingController;

  /// 搜索
  get _search => Container(
    width: 202,
    // height: 64,
    padding: EdgeInsets.symmetric(vertical: 0, horizontal: 10),
    decoration: BoxDecoration(
      color: colorWhite2,
      borderRadius: BorderRadius.all(Radius.circular(32)),
    ),
    child: Row(
      children: [
        _searchIcon,
        hiSpace(width: 10),
        Expanded(child: _textField)
      ],
    ),
  );

  /// 搜索图标
  get _searchIcon => SvgPicture.asset(
    "images/package/package_search_icon.svg",
    width: 16,
    height: 16,
  );

  /// 搜索输入框
  get _textField => TextField(
    // 控制器
    controller: _textEditingController,
    // 焦点控制
    focusNode: _focusNode,
    // 设置光标颜色
    cursorColor: colorBlue1,
    // 光标高度
    cursorHeight: 16,
    // 样式
    style: FontUtil.fontGrey5(16),
    // 装饰
    decoration: InputDecoration(
      // 去掉textField自定的高度
      isCollapsed: true,
      // 内容边距
      contentPadding: EdgeInsets.only(bottom: 1),
      // 去除边框
      border: InputBorder.none,
      // 提示语句
      hintText: "搜索关键字",
      // 提示语句样式
      hintStyle: FontUtil.fontGrey5(16),
    ),
  );

  @override
  void initState() {
    super.initState();

    _focusNode = FocusNode();
    _textEditingController = TextEditingController();

    // 焦点监听
    _focusNode.addListener(() {
      if (!_focusNode.hasFocus) {
        String text = _textEditingController.text;
        widget.onSubmittedFun(text);
      }
    });
  }

  @override
  Widget build(BuildContext context) {

    return Container(
      padding: EdgeInsets.symmetric(vertical: 12),
      height: 60,
      decoration: BoxDecoration(
          color: colorWhite1,
          border: Border(bottom: BorderSide(color: colorBlack5, width: 1))),
      child: Row(
        children: [
          BackButtonWidget.back(pageContext: widget.pageContext,size: 26,onTapFun: () {
            if(Navigator.canPop(context)){
              Navigator.pop(context);
            }else{
            //   退出还是无动作呢
            }
          },),
          hiSpace(width: 5),
          Text(
            widget.titleName,
            style: FontUtil.fontBlack1(18, fontWeight: FontWeight.w500),
          ),
          hiSpace(width: 10),
          _search,
        ],
      ),
    );
  }
}
